package com.itheima.restkeeper.service.impl;

import com.alibaba.druid.sql.visitor.functions.If;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.restkeeper.constant.SuperConstant;
import com.itheima.restkeeper.pojo.Brand;
import com.itheima.restkeeper.mapper.BrandMapper;
import com.itheima.restkeeper.req.BrandVo;
import com.itheima.restkeeper.service.IBrandService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.restkeeper.utils.BeanConv;
import com.itheima.restkeeper.utils.EmptyUtil;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * @Description：品牌管理 服务实现类
 */
@Service
public class BrandServiceImpl extends ServiceImpl<BrandMapper, Brand> implements IBrandService {

    /**
     * 分页查询核心品牌列表
     * @param brandVo 查询条件
     * @param pageNum 页码
     * @param pageSize 每页条数
     * @return
     */
    @Override
    public Page<Brand> findBrandVoPage(BrandVo brandVo, int pageNum, int pageSize) {
        //构建分页对象
        Page<Brand> page = new Page<>(pageNum, pageSize);
        //构建查询条件
        LambdaQueryWrapper<Brand> queryWrapper = Wrappers.<Brand>lambdaQuery();
        //按品牌名称查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getBrandName())) {
            queryWrapper.like(Brand::getBrandName,brandVo.getBrandName());
        }
        //按品牌分类查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getCategory())) {
            queryWrapper.eq(Brand::getCategory,brandVo.getCategory());
        }
        //按品牌状态查询
        if (!EmptyUtil.isNullOrEmpty(brandVo.getEnableFlag())) {
            queryWrapper.eq(Brand::getEnableFlag,brandVo.getEnableFlag());
        }
        //按创建时间降序
        queryWrapper.orderByDesc(Brand::getCreatedTime);
        //执行分页查询
        return page(page,queryWrapper);
    }

    /**
     * 新增品牌
     * @param brandVo 对象信息
     * @return
     */
    @Override
    public Brand createBrand(BrandVo brandVo) {
        //转换BrandVo为Brand
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        //执行保存
        return this.save(brand)?brand:null;
    }

    /**
     * 修改品牌
     * @param brandVo 对象信息
     * @return
     */
    @Override
    public Boolean updateBrand(BrandVo brandVo) {
        //转换BrandVo为Brand
        Brand brand = BeanConv.toBean(brandVo, Brand.class);
        return updateById(brand);
    }

    /**
     * 批量删除品牌
     * @param checkedIds 选择中对象Ids
     * @return
     */
    @Override
    public Boolean deleteBrand(String[] checkedIds) {
        //转换数组为集合
        List<String> list = Arrays.asList(checkedIds);
        List<Long> longs = new ArrayList<>();
        list.forEach(n->{
            longs.add(Long.valueOf(n));
        });
        return removeByIds(longs);
    }

    /**
     * 品牌下拉列表
     * @return
     */
    @Override
    public List<Brand> findBrandVoList() {
        //构建查询条件
        //查询有效状态
        return list(Wrappers.<Brand>lambdaQuery().eq(Brand::getEnableFlag,SuperConstant.YES));
    }
}
